package com.ambitor.ipro.app;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.ambitor.grass.util.data.IData;
import com.ambitor.ipro.app.gen.TaskGenerator;
import com.ambitor.ipro.app.sche.TaskCarrier;
import com.ambitor.ipro.app.sche.TaskConsumer;

public class TaskApp {

	public static void main(String[] args) {
		taskScheduler();
		taskGenerator();
	}

	private static void taskScheduler() {
		BlockingQueue<IData> taskQueue = new LinkedBlockingQueue<IData>();
		Thread ProductThread = new Thread(new TaskCarrier(taskQueue));
		ProductThread.start();

		Thread ConsumerThread1 = new Thread(new TaskConsumer(taskQueue));
		ConsumerThread1.start();

	    Thread ConsumerThread2 = new Thread(new TaskConsumer(taskQueue));
		ConsumerThread2.start();
		//
		// Thread ConsumerThread3 = new Thread(new TaskConsumer(taskQueue));
		// ConsumerThread3.start();
	}

	private static void taskGenerator() {
		new TaskGenerator().run();

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1); // 当前时间加1天
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd 00:30:00");
		cal.setTime(Timestamp.valueOf(fmt.format(cal.getTime())));
		long period = 1000 * 60 * 60 * 24;
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TaskGenerator(), cal.getTime(), period);
	}

}
